package com.punai.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.constant.UserConstants;
import com.punai.common.core.domain.entity.EcoCompany;
import com.punai.common.core.domain.entity.EcoCompanyDept;
import com.punai.common.core.domain.entity.SysUser;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.SecurityUtils;
import com.punai.common.utils.StringUtils;
import com.punai.common.utils.spring.SpringUtils;
import com.punai.system.mapper.*;
import com.punai.system.service.IEcoCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class EcoCompanyServiceImpl implements IEcoCompanyService {
    @Autowired
    private EcoCompanyMapper ecoCompanyMapper;
    @Autowired
    private EcoCompanyDeptMapper ecoCompanyDeptMapper;

    @Override
    public Integer getMaxSort() {
        return ecoCompanyMapper.getMaxSort();
    }

    @Override
    public List<EcoCompany> selectCompanyList(EcoCompany company) {
        company.setCompanyId(SecurityUtils.getCompanyId());
        return ecoCompanyMapper.selectCompanyList(company);
    }

    @Override
    public void checkCompanyDataScope(String id) {
        if (!SysUser.isAdmin(SecurityUtils.getUserId()))
        {
            EcoCompany company = new EcoCompany();
            company.setId(id);
            List<EcoCompany> companyList = SpringUtils.getAopProxy(this).selectCompanyList(company);
            if (StringUtils.isEmpty(companyList))
            {
                throw new ServiceException("没有权限访问企业数据！");
            }
        }
    }

    @Override
    public EcoCompany selectCompanyById(String id) {
        return ecoCompanyMapper.selectCompanyById(id);
    }

    @Override
    public List<String> selectDeptById(String id) {
        List<EcoCompanyDept> deptList = ecoCompanyDeptMapper.selectList(Wrappers.<EcoCompanyDept>lambdaQuery()
                .eq(EcoCompanyDept::getCompanyId, id));
        return deptList.stream().map(EcoCompanyDept::getDeptId).collect(Collectors.toList());
    }

    @Override
    public String checkCompanyNameUnique(EcoCompany company) {
        String id = StringUtils.isNull(company.getId()) ? "-1L" : company.getId();
        String companyName = company.getCompanyName();
        LambdaQueryWrapper<EcoCompany> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EcoCompany::getCompanyName,companyName);
        EcoCompany sysCompany = ecoCompanyMapper.selectOne(queryWrapper);
        if (ObjectUtil.isNotNull(sysCompany) && !sysCompany.getId().equals(id)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkCompanyPrxUnique(EcoCompany company) {
        String prx = company.getPrx();
        LambdaQueryWrapper<EcoCompany> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EcoCompany::getPrx,prx);
        Long count = ecoCompanyMapper.selectCount(queryWrapper);
        if (count > 0) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    @Transactional
    public int insertCompany(EcoCompany company) {
        int insert ;
        company.setCompanyId(SecurityUtils.getCompanyId());
        company.setCompanySort(getMaxSort()+1);
        insert = ecoCompanyMapper.insert(company);
        List<String> deptIds = company.getDeptIds();
        if (CollUtil.isNotEmpty(deptIds)){
            for (String deptId : deptIds) {
                EcoCompanyDept dept = new EcoCompanyDept();
                dept.setCompanyId(company.getId());
                dept.setDeptId(deptId);
                insert = ecoCompanyDeptMapper.insert(dept);
            }
        }
        return insert;
    }

    @Override
    public int selectNormalChildrenCompanyById(Long id) {
        return 0;
    }

    @Override
    @Transactional
    public int updateCompany(EcoCompany company) {
        int i;
        i = ecoCompanyMapper.updateById(company);
        ecoCompanyDeptMapper.delete(Wrappers.<EcoCompanyDept>lambdaQuery().eq(EcoCompanyDept::getCompanyId, company.getId()));
        List<String> deptIds = company.getDeptIds();
        if (CollUtil.isNotEmpty(deptIds)){
            for (String deptId : deptIds) {
                EcoCompanyDept dept = new EcoCompanyDept();
                dept.setCompanyId(company.getId());
                dept.setDeptId(deptId);
                i = ecoCompanyDeptMapper.insert(dept);
            }
        }
        return i;
    }

    @Override
    public boolean hasChildByCompanyId(Long deptId) {
        return false;
    }

    @Override
    public boolean checkCompanyExistUser(Long id) {
        return false;
    }

    @Override
    @Transactional
    public int deleteCompanyById(String id) {
        int delete;
        delete = ecoCompanyMapper.delete(Wrappers.<EcoCompany>lambdaQuery().eq(EcoCompany::getId, id));
        delete = ecoCompanyDeptMapper.delete(Wrappers.<EcoCompanyDept>lambdaQuery().eq(EcoCompanyDept::getCompanyId, id));
        return delete;
    }

    @Override
    public EcoCompany treeCompanyList() {
        return null;
    }

    @Override
    public List<Map<String, String>> getListType() {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>() {{
            add(new HashMap<String, String>() {{
                put("val", "土建");
                put("key", "T");
            }});
            add(new HashMap<String, String>() {{
                put("val", "专业");
                put("key", "Z");
            }});
        }};
        return list;
    }

    @Override
    public List<Map<String, String>> getZyType() {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>() {{
            add(new HashMap<String, String>() {{
                put("val", "路桥公司");
                put("key", "L");
            }});
            add(new HashMap<String, String>() {{
                put("val", "安装公司");
                put("key", "A");
            }});
            add(new HashMap<String, String>() {{
                put("val", "园林公司");
                put("key", "Y");
            }});
            add(new HashMap<String, String>() {{
                put("val", "装饰公司");
                put("key", "Z");
            }});
            add(new HashMap<String, String>() {{
                put("val", "钢结构公司");
                put("key", "G");
            }});
            add(new HashMap<String, String>() {{
                put("val", "其他类型");
                put("key", "Q");
            }});
        }};
        return list;
    }
}
